(add-to-list 'load-path "~/.emacs.d")
(add-to-list 'load-path "~/.emacs.d/muse-3.20/lisp")
(remove-hook 'find-file-hooks 'vc-find-file-hook)
(setq vc-handled-backends nil)
(if (window-system) (set-frame-size (selected-frame) 144 50)) 
(require 'vimpulse)
(require 'hide-region)
(require 'muse)
(require 'muse-colors)
(require 'muse-mode)
(require 'muse-wiki)
(require 'grab-and-drag)
(grab-and-drag-mode 1)
(setq grab-and-drag-pointer-shape nil)
(vimpulse-global-set-key 'vi-state (kbd "C-d") '(lambda ()(interactive)(if 
															 (= (count-windows) 1) 
															 (save-buffers-kill-terminal)
															 (delete-window)
															 )
												  ))
(vimpulse-global-set-key 'vi-state (kbd ";") 'viper-ex)
(vimpulse-global-set-key 'vi-state (kbd "C-e") 'viper-goto-eol)
(vimpulse-global-set-key 'vi-state (kbd "M-C-h") 'shrink-window-horizontally)
(vimpulse-global-set-key 'vi-state (kbd "M-C-l") 'enlarge-window-horizontally)
(vimpulse-global-set-key 'vi-state (kbd "M-C-j") 'shrink-window)
(vimpulse-global-set-key 'vi-state (kbd "M-C-k") 'enlarge-window)
(vimpulse-map (kbd "M-k") 'windmove-up)
(vimpulse-map (kbd "M-j") 'windmove-down)
(vimpulse-map (kbd "M-l") 'windmove-right)
(vimpulse-map (kbd "M-h") 'windmove-left)
(vimpulse-global-set-key 'vi-state (kbd "M-i") '(lambda () (interactive)(hide-region-hide)(vimpulse-visual-toggle-line)))
(vimpulse-global-set-key 'vi-state (kbd "M-o") 'hide-region-unhide)
(vimpulse-global-set-key 'vi-state (kbd "M-k") 'toggle-selective-display-column )

(vimpulse-global-set-key 'vi-state (kbd "M-n") 'gud-next)
(vimpulse-global-set-key 'vi-state (kbd "M-m") 'gud-step)
(vimpulse-global-set-key 'vi-state (kbd "M-C-m") 'gud-up)
(vimpulse-global-set-key 'vi-state (kbd "M-C-n") 'gud-down)
(add-to-list 'ex-token-alist '("b" (t_selectbuffer)))
(add-to-list 'ex-token-alist '("t" (t_tagsearch)))

(defun count-windows (&optional minibuf)
  "Return the number of visible windows in selected frame.
Optional arg MINIBUF is used only if selected frame has a minibuffer."
  (let ((count 0))
    (walk-windows (function (lambda (w) (setq count (+ count 1))))
                  (and (memq (cdr (assoc 'minibuffer (frame-parameters)))
                             '(only t)) ; If this frame has a minibuffer,
                       minibuf))        ; pass the arg.  (Else pass nil.)
    count))
(defun auto-tags-appros ()
  (interactive)
  (if (region-active-p)
	  (progn
		(setq myStr (buffer-substring (region-beginning) (region-end)))
		(message myStr)
		(tags-apropos myStr)
		)
	(progn
	  (vimpulse-inner-word 1)
	  (if (region-active-p)
		  (progn
			(setq myStr (buffer-substring (region-beginning) (region-end)))
			(message myStr)
			(tags-apropos myStr)
	  )
	)
)
	)
  )
(defun vim () (interactive) (shell-command (concat "echo 'vim +" (substring (what-line) 5) " \"" (buffer-file-name) "\"'> /dev/shm/vcommand"))(save-buffers-kill-terminal))
(defun cdw () (interactive) (cd "/home/taesoobear/taesoo_cmu/OgreFltk/work"))
(defun cdt () (interactive) (cd "/home/taesoobear/taesoo_cmu"))
(defun gdb-ipc() (interactive) (cdw)(gud-gdb "gdb --fullname --args ogrefltksimple_d rigidbodywin.lua IPC_based/driverKinematic"))
(defun gdb-ipcf() (interactive) (cdw)(gud-gdb "gdb --fullname --args ogrefltksimple_d rigidbodywin.lua IPC_based/driverDynamic"))
(defun gdb-console() (interactive) (cdw)(gud-gdb "gdb --fullname --args ogrefltksimple_d rigidbodywin.lua subRoutines/console"))
(add-to-list 'ex-token-alist '("ts" (auto-tags-appros)))
;(vimpulse-global-set-key 'vi-state (kbd "C-w t") 'auto-tags-appros)
(setq-default tags-file-name "~/taesoo_cmu/TAGS")
(defun t_selectbuffer()
  (interactive)
  (shell)
  (server-force-delete)
  (server-start)
  (insert "gitv --emacs vir ")
)
(defun t_tagsearch()
  (interactive)
  (vimpulse-inner-word 1)
  (setq myStr (buffer-substring (region-beginning) (region-end)))
  (deactivate-mark)
  (bookmark-set "tagsearch")
  (shell)
  (server-force-delete)
  (server-start)
  (insert (concat "gitv --emacs ts " myStr))
)
(vimpulse-global-set-key 'vi-state (kbd "C-w b") '(lambda() (interactive)(list-buffers)(other-window 1)))
(defadvice save-buffers-kill-emacs (around no-query-kill-emacs activate)
  "Prevent annoying \"Active processes exist\" query when you quit Emacs."
  (flet ((process-list ())) ad-do-it))
(require 'lua-mode)
(require 'python-mode)
(add-to-list 'auto-mode-alist '("\\.lua$" . lua-mode)) 
(add-to-list 'auto-mode-alist '("\\.wiki$" . muse-mode))
(autoload 'lua-mode "lua-mode" "Lua editing mode." t)
(add-hook 'lua-mode-hook 'hs-minor-mode)
(defun taesoo-c++-indent-setup()
  (setq c-basic-offset 4)
  (setq tab-width 4)
  (setq c-brace-offset -8)
  (setq c-indent-level 4)
  (setq c-continued-statement-offset 4)
  (setq c-brace-imaginary-offset -4)
  (setq c-label-offset -4)
  (setq c-argdecl-indent 4)
)

(add-hook 'c-mode-common-hook   'taesoo-c++-indent-setup)
(add-hook 'c-mode-common-hook   'hs-minor-mode)
(add-hook 'emacs-lisp-mode-hook 'hs-minor-mode)
(add-hook 'java-mode-hook       'hs-minor-mode)
(add-hook 'lisp-mode-hook       'hs-minor-mode)
(add-hook 'perl-mode-hook       'hs-minor-mode)
(add-hook 'sh-mode-hook         'hs-minor-mode)

(global-set-key (kbd "M-r") 'hs-toggle-hiding)
(setq lua-indent-level 4) ; by default 3 -.-
(setq-default tab-width 4)
(setq scroll-step           1
         scroll-conservatively 10000)
(setq-default default-truncate-lines t) 
(setq read-file-name-completion-ignore-case t)
(setq x-select-enable-clipboard t) 
(setq interprogram-paste-function 'x-cut-buffer-or-selection-value)
(custom-set-faces
  ;; custom-set-faces was added by Custom.
  ;; If you edit it by hand, you could mess it up, so be careful.
  ;; Your init file should contain only one such instance.
  ;; If there is more than one, they won't work right.
 '(default ((t (:inherit nil :stipple nil :background "black" :foreground "lightgrey" :inverse-video nil :box nil :strike-through nil :overline nil :underline nil :slant normal :weight normal :height 132 :width normal :foundry "unknown" :family "DejaVu Sans Mono")))))
;(require 'color-theme) ; color-theme was too slow. 
;(color-theme-initialize)
;(color-theme-wheat)
(tool-bar-mode -1)
;(scroll-bar-mode -1)
;(menu-bar-mode 0)
(custom-set-variables
  ;; custom-set-variables was added by Custom.
  ;; If you edit it by hand, you could mess it up, so be careful.
  ;; Your init file should contain only one such instance.
  ;; If there is more than one, they won't work right.
 '(default-input-method "korean-hangul")
 '(inhibit-startup-screen t)
 '(show-paren-mode t)
 '(tool-bar-mode nil))

(defun toggle-selective-display-column ()
  "set selective display fold everything greater than the current column, or toggle off if active"
  (interactive)
  (set-selective-display
   (if selective-display nil (or (+ (current-column) 1) 1))))
; needed for emacs -nw users like me
(defun yank-to-x-clipboard ()
  (interactive)
  (if (region-active-p)
	(progn
	  (shell-command-on-region (region-beginning) (region-end) "xsel -i")
	  (message "Yanked region to clipboard!")
	  (deactivate-mark))
	(progn
	  (insert (shell-command-to-string "xsel"))
	  (message "Pasted from clipboard!")
	  )
	)
  )

(global-set-key [f3] '(lambda ()(interactive)(bookmark-jump "tagsearch")(vimpulse-visual-mode -1)))
(global-set-key [f4] 'shell)
(global-set-key [f5] 'auto-tags-appros)
(global-set-key [f6] 'list-buffers)
(defun t_serverstart() (interactive) (server-start) (message "server started"))
(global-set-key [f7] 't_serverstart)
(global-set-key [f8] 'yank-to-x-clipboard)
(defun t_save() (interactive)(save-buffer)(viper-change-state-to-vi)) 
(global-set-key (kbd "\C-s") 't_save)
(global-set-key [f9] 'save-buffers-kill-terminal)
(global-set-key [f2] 'toggle-selective-display-column)

(require 'xclip)

(defun command-line-diff (switch)
      (let ((file1 (pop command-line-args-left))
            (file2 (pop command-line-args-left)))
        (ediff file1 file2)))
    
;; Usage: emacs -diff file1 file2
(add-to-list 'command-switch-alist '("-diff" . command-line-diff))
;(defun autocompile nil
;  "compile itself if ~/.emacs"
;  (interactive)
;  (require 'bytecomp)
;  (let ((dotemacs (expand-file-name "~/.emacs")))
;    (if (string= (buffer-file-name) (file-chase-links dotemacs))
;      (byte-compile-file dotemacs))))
; 
;(add-hook 'after-save-hook 'autocompile)
(set-language-environment "Korean") 
(prefer-coding-system 'utf-8)
(set-fontset-font "fontset-default" '(#x1100 . #xffdc)  '("NANumGothicCoding" . "unicode-bmp")) ;;; 유니코드 한글영역...NanumGothicCoding에다가 원하는폰트를 적는다
(set-input-method "korean-hangul")
